import { Injectable } from '@angular/core';
import { CodePush, SyncStatus, InstallMode } from '@ionic-native/code-push';
import { DialogProvider } from '../../providers/dialog/dialog';
import { EventProvider } from '../../providers/event/event';
/*
  Generated class for the CodepushProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class CodepushProvider {

  constructor(
    public codePush: CodePush,
    public dialog: DialogProvider,
    public event: EventProvider) {
    console.log('Hello CodepushProvider Provider');
  }

  public initCodePush() {

    //对象写入
    let dialogs = this.dialog;
    let events = this.event;

    //下载进度条
    const downloadProgress = (progress) => {
      //下载进度
      let progressvalue = parseInt((progress.receivedBytes / progress.totalBytes*100).toString());
      events.sendEvents(events.EVENT_PROGRESS_VALUE, { "status": 1, "value": progressvalue });
      console.log(`Downloaded ${progress.receivedBytes} of ${progress.totalBytes}`);
    }

    //异步提交
    this.codePush.sync({
      ignoreFailedUpdates: true,
      installMode: InstallMode.IMMEDIATE,
      minimumBackgroundDuration: 0,
      mandatoryInstallMode: InstallMode.IMMEDIATE,
      updateDialog: {
        optionalIgnoreButtonLabel: '跳过更新',
        optionalInstallButtonLabel: '执行更新',
        optionalUpdateMessage: '检测到新版本，是否更新？',
        mandatoryUpdateMessage: "您的版本过旧，请更新",
        mandatoryContinueButtonLabel: "执行更新",
        updateTitle: '更新提示',
      }
    }, downloadProgress).subscribe((syncStatus) => {
      //是否执行更新
      if (syncStatus === SyncStatus.UP_TO_DATE) {//0
        //无需更新 
        console.log(`无需更新`);
      } else {
        //条件显示
        switch (syncStatus) {
          case SyncStatus.UPDATE_INSTALLED:
            //开始解压安装 第五步
            console.log(`开始解压安装`);
            break;
          case SyncStatus.UPDATE_IGNORED:
            //更新取消 
            console.log(`更新取消`);
            break;
          case SyncStatus.ERROR:
            //更新异常
            console.log(`更新异常`);
            break;
          case SyncStatus.IN_PROGRESS:
            //进入进度条
            console.log(`进入进度条`);
            break;
          case SyncStatus.CHECKING_FOR_UPDATE:
            //检查更新 第一步
            console.log(`检查更新`);
            break;
          case SyncStatus.AWAITING_USER_ACTION:
            //等待用户选择 第二步
            console.log(`等待用户选择`);
            break;
          case SyncStatus.DOWNLOADING_PACKAGE:
            //开始下载安装包--执行进度条显示 第三步
            console.log(`开始下载安装包`);
            dialogs.showPopoer("ProgressPage", {}, { enableBackdropDismiss: false, cssClass: "progressbg" }, null);
            break;
          case SyncStatus.INSTALLING_UPDATE:
            //下载完毕开始安装 第四步
            console.log(`下载完毕开始安装`);
            break;
          default:
            break;
        }
      }
    },
      (error) => {
        events.sendEvents(events.EVENT_PROGRESS_VALUE, { "status": 0, "value": 100 });
      });
  }

}
